import axios from 'axios'
// import store from '@/store'
import {
  VueAxios
} from './axios'
import notification from 'ant-design-vue/es/notification'
import {
  ACCESS_TOKEN
} from '@/store/mutation-types'

// 创建 axios 实例
const service = axios.create({
  baseURL: process.env.API_BASE_URL, // api 的 base_url
  timeout: 6000 // 请求超时时间
})

const err = (error) => {
  console.log('request js')
  console.log(error.response)
  if (error.response) {
    const data = error.response.data
    if (error.response.status === 401) {
      notification.error({
        message: '无权限',
        description: data.msg,
        duration: 0
      })
    }

    if (error.response.status === 500) {
      notification.error({
        message: '服务器内部异常',
        description: data.msg,
        duration: 0
      })
    }
  }
  return Promise.reject(error)
}

// request interceptor
service.interceptors.request.use(config => {
  const token = sessionStorage.getItem(ACCESS_TOKEN)
  config.headers['YM-SOURCE'] = 'MANAGE'
  if (token) {
    config.headers['YM-ACCESS-TOKEN'] = token // 让每个请求携带自定义 token 请根据实际情况自行修改
  }
  return config
}, err)

// response interceptor
service.interceptors.response.use((response) => {
  return response.data
}, err)

const installer = {
  vm: {},
  install (Vue) {
    Vue.use(VueAxios, service)
  }
}

export {
  installer as VueAxios,
  service as axios
}
